<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { ExhibitionVO } from '#/api/system/exhibition/model';

import { useAccess } from '@vben/access';
import { Page, useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { Modal, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import {
  exhibitionPage,
  exhibitionRemove,
  exhibitionUpdateDefault,
  exhibitionUpdateStatus,
} from '#/api/system/exhibition';
import { TableSwitch } from '#/components/table';

import { columns, querySchema } from './data';
import exhibitionDrawer from './exhibition-drawer.vue';

const { hasAccessByCodes } = useAccess();

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await exhibitionPage({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'exhibition-list-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [ExhibitionDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: exhibitionDrawer,
});

function handleAdd() {
  drawerApi.setData({});
  drawerApi.open();
}

async function handleEdit(row: ExhibitionVO) {
  drawerApi.setData({ id: row.id });
  drawerApi.open();
}

async function handleDelete(row: ExhibitionVO) {
  await exhibitionRemove(row.id);
  await tableApi.query();
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: ExhibitionVO) => row.id);
  Modal.confirm({
    title: '提示',
    okType: 'danger',
    content: `确认删除选中的${ids.length}条记录吗？`,
    onOk: async () => {
      await exhibitionRemove(ids);
      await tableApi.query();
    },
  });
}

// 设置是否默认
async function defaultUpdate(row: ExhibitionVO) {
  const data = {
    id: row.id,
    isDefault: row.isDefault,
  };
  await exhibitionUpdateDefault(data);
}

async function statusUpdate(row: ExhibitionVO) {
  const data = {
    id: row.id,
    status: row.status,
  };
  await exhibitionUpdateStatus(data);
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable>
      <template #table-title>
        <main-title text="展会列表" />
      </template>
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            v-access:code="['system:exhibition:remove']"
            @click="handleMultiDelete"
          >
            {{ $t('pages.common.delete') }}
          </a-button>
          <a-button
            type="primary"
            v-access:code="['system:exhibition:add']"
            @click="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #isDefault="{ row }">
        <TableSwitch
          v-model:value="row.isDefault"
          :api="() => defaultUpdate(row)"
          :checked-value="1"
          :un-checked-value="0"
          checked-text="默认"
          un-checked-text=""
          :disabled="!hasAccessByCodes(['system:exhibition:edit'])"
          @reload="tableApi.query()"
        />
      </template>
      <template #status="{ row }">
        <TableSwitch
          v-model:value="row.status"
          :api="() => statusUpdate(row)"
          :disabled="!hasAccessByCodes(['system:exhibition:edit'])"
          @reload="tableApi.query()"
        />
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['system:exhibition:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['system:exhibition:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <ExhibitionDrawer @reload="tableApi.query()" />
  </Page>
</template>
